const express = require('express');
var router = express.Router();
const {md5,getUnix} = require('../../model/tools')
const ManagerModel = require('../../model/managerModel')

//管理员列表
router.get("/",(req,res)=>{
  //获取管理员数据
  ManagerModel.find({},(err,data)=>{
    if(err){
      console.log(err);
      return;
    }
    res.render("admin/manager/index.html",{
      lists:data
    });
  })
  
})

router.get("/add",(req,res)=>{
  res.render("admin/manager/add.html");
})
//增加管理员逻辑
router.post("/doAdd",(req,res)=>{
  //获取表单数据
  let {username,password,rpassword,mobile,email,status} = req.body;

  //验证数据是否合法
  if(!username){
    res.render("admin/public/error.html",{
			redirectUrl:`/${req.app.locals.adminPath}/manager/add`,
			message:"用户名不能为空"
		})
    return;
  }
  if(password.length<6){
    res.render("admin/public/error.html",{
			redirectUrl:`/${req.app.locals.adminPath}/manager/add`,
			message:"密码不能小于6位"
		})
    return;
  }
  if(password!=rpassword){
    res.render("admin/public/error.html",{
      redirectUrl:`/${req.app.locals.adminPath}/manager/add`,
      message:"两次密码不一致"
    });
    return;
  }
  //判断数据库中是否有相同的用户名
  ManagerModel.find({username},(err,data)=>{
    if(err){
      console.log(err);
      return;
    }
    if(data.length>0){
      res.render("admin/public/error.html",{
        redirectUrl:`/${req.app.locals.adminPath}/manager/add`,
        message:"用户名已存在"
      });
      return;
    }
    //保存数据
    var addResult = new ManagerModel({
      username,
      password:md5(password),
      mobile,
      email,
      addtime:getUnix(),
      status
    });
    addResult.save((err)=>{
      if(err){
        console.log(err);
        return;
      }
      res.redirect(`/${req.app.locals.adminPath}/manager/`);
    })
  })
})

//管理员数据回显
router.get("/edit",(req,res)=>{
  //获取要修改的管理员id
  let {id} = req.query;
  ManagerModel.find({_id:id},(err,data)=>{
    if(err){
      console.log(err);
      return;
    }
    if(data.length>0){
      res.render("admin/manager/edit.html",{
        list:data[0]
      });
    }else{
      res.redirect(`/${req.app.locals.adminPath}/manager/`);
    }
 
  })

})

//修改管理员逻辑
router.post("/doEdit",(req,res)=>{
  //获取表单数据
  // eslint-disable-next-line no-unused-vars
  let {id,username,mobile,email,password,rpassword,status} = req.body;

  //判断密码是否修改
  if(password != '******'){//密码修改
    if(password.length<6){
      res.render("admin/public/error.html",{
        redirectUrl:`/${req.app.locals.adminPath}/manager/edit?id=${id}`,
        message:"密码不能小于6位"
      });
      return;
    }
    if(password!=rpassword){
      res.render("admin/public/error.html",{
        redirectUrl:`/${req.app.locals.adminPath}/manager/edit?id=${id}`,
        message:"两次密码不一致"
      });
      return;
    }
    ManagerModel.updateOne({_id:id},{
      email,
      mobile,
      status,
      password:md5(password),
    },(err)=>{
      if(err){
        console.log(err);
        return;
      }
      res.redirect(`/${req.app.locals.adminPath}/manager/`);
    })
  }else{//密码未修改
    ManagerModel.updateOne({_id:id},{
      email,
      mobile,
      status
    },(err)=>{
      if(err){
        console.log(err);
        return;
      }
      res.redirect(`/${req.app.locals.adminPath}/manager/`);
    })
  }
})

//删除管理员
router.get("/delete",(req,res)=>{
  //获取要删除的管理员id
  let {id} = req.query;
  ManagerModel.deleteOne({_id:id},(err)=>{
    if(err){
      console.log(err);
      return;
    }
    res.render("admin/public/success.html",{
			redirectUrl:`/${req.app.locals.adminPath}/manager/`,
			message:"删除成功"
		})
  })

})
module.exports = router